const http = require('http');
const url = require('url');
const ss = require('./06StaticServer');
const querystring = require('querystring');

http.createServer((req, resp) => {
    // 启动静态资源服务
    if (req.url.startsWith('/www')) {
        ss.staticServer(req, resp, __dirname);
    }
    if (req.url.startsWith('/login')) {
        if (req.method == 'GET') {
            let param = url.parse(req.url, true).query;
            if (param.username == 'admin' && param.password == '123') {
                resp.end('get success');
            } else {
                resp.end('get failure');
            }
        }
        if (req.method == 'POST') {
            let pdata = '';
            req.on('data', (chunk) => {
                pdata += chunk;
            });
            req.on('end', () => {
                let param = querystring.parse(pdata);
                if (param.username == 'admin' && param.password == '123') {
                    resp.end('post success');
                } else {
                    resp.end('post failure');
                }
            });
        }
    }
}).listen(3000, ()=>{
    console.log('running...');
});